package org.linlinjava.litemall.admin.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallAdmin;
import org.linlinjava.litemall.db.domain.LitemallWithdraw;
import org.linlinjava.litemall.db.service.LitemallWithdrawService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/admin/withdraw")
@Validated
public class AdminWithdrawController {
    private final Log logger = LogFactory.getLog(AdminWithdrawController.class);

    @Autowired
    private LitemallWithdrawService withdrawService;

    @RequiresPermissions("admin:withdraw:list")
    @RequiresPermissionsDesc(menu = {"分销中心", "提现管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(@RequestParam(defaultValue = "")  String username,
                       @RequestParam(defaultValue = "")  Integer userId,
                       @RequestParam(defaultValue = "")  Byte status,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "update_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        List<LitemallWithdraw> userList = withdrawService.queryAdminList(username, userId, status, page, limit, sort, order);
        return ResponseUtil.okList(userList);
    }
    @RequiresPermissions("admin:withdraw:list")
    @RequiresPermissionsDesc(menu = {"分销中心", "提现管理"}, button = "详情")
    @GetMapping("/detail")
    public Object detail(@NotNull Integer id) {
        LitemallWithdraw user=withdrawService.findById(id);
        return ResponseUtil.ok(user);
    }
    @RequiresPermissions("admin:withdraw:update")
    @RequiresPermissionsDesc(menu = {"分销中心", "提现管理"}, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody LitemallWithdraw user) {
        Subject currentUser = SecurityUtils.getSubject();
        LitemallAdmin admin = (LitemallAdmin) currentUser.getPrincipal();
        int code = withdrawService.updateByStatus(admin.getUsername(),user);
        return ResponseUtil.ok(code);
    }
}
